﻿using Calculation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpRunner
{
    public class Runner : CalculationRunner
    {
        double result;
        uint size;

        public Runner(uint size)
        {
            this.size = size;
        }

		public override double Result
		{
            get { return result; }
		}

        public override void Run(CalculationProgress progress)
        {
            ulong inside = 0, samples = 0;
            double pi = 0;
            MersenneTwister engine = new MersenneTwister();
            for(;;)
            {
                for (int i = 0; i < size; ++i, ++samples)
                {
                    double x = engine.Next();
                    double y = engine.Next();
                    if (x * x + y * y <= 1)
                        ++inside;     
                }
                pi = 4.0 * (double)inside / samples;
                progress.SetCurrentResult(pi, samples);
                if (progress.CancellationPending)
                    break;
            }
            result = pi;
        }
    }
}
